<?php
namespace Admin\Controller;
use Think\Controller;
class ProductController extends BaseController {
    public function product_list(){
    	if(I('post.')){
		    $btn=I('post.btn');
		    $idarr=I('post.idarr');
		    $idstr=implode(',',$idarr);
		    if($btn==1 && !empty($idstr)){
		    	// 批量删除图片
		    	$old_arr=M('product')
			    			->field("pro_img1")
			    			->where("pro_id IN($idstr)")
			    			->select();
			   	foreach($old_arr as $v){
			   		$old=$v['pro_img1'];
			   		$path='./Upload/'.$v['pro_img1'];
			   		if(!empty($old) && file_exists($path)){
						unlink($path);
					}
			   	}
				
				
				$old_arr2=M('product')
							->field("pro_img2")
							->where("pro_id IN($idstr)")
							->select();
				foreach($old_arr2 as $v){
					$old2=$v['pro_img2'];
					$path2='./Upload/'.$v['pro_img2'];
					if(!empty($old2) && file_exists($path2)){
						unlink($path2);
					}
				}

		      $res=M('product')
		      		->where("pro_id IN($idstr)")
		      		->delete();
		      if($res){
		        $this->success('批量删除成功',U('Admin/Product/product_list'));
		      }else{
		        $this->error('数据输入有误，请重新删除');
		      }
		    }
		  }
		$this->display();
    }
    public function product_ajax(){
    	$limit=5;
		if(I('get.sou')){
			$pro_name=I('get.sou');
			$count=M('product')->where("pro_name like '%$pro_name%'")->count();
		}else{
			$count=M('product')->count();
		}
		$page=new \Think\Page($count,$limit);
		$show=$page->show();
		$ord=I('get.ord');
		if($ord==1){
			$order="a.pro_ord ASC";
		}elseif($ord==2){
			$order="a.pro_ord DESC";
		}else{
			$order="a.pro_addtime DESC,a.pro_id DESC";
		}
		if(I('get.sou')){
			$pro_name=I('get.sou');
			$product=M('product')
					->alias('a')
					->field('a.*,c.cat_name')
					->join("ft_cat_pro as c on a.cat_id=c.cat_id")
					->where("a.pro_name like '%$pro_name%'")
					->order($order)
					->limit($page->firstRow.','.$page->listRows)
					->select();
		}else{
			$product=M('product')
					->alias('a')
					->field('a.*,c.cat_name')
					->join("ft_cat_pro as c on a.cat_id=c.cat_id")
					->where("a.pro_name like '%$pro_name%'")
					->order($order)
					->limit($page->firstRow.','.$page->listRows)
					->select();
		}
		$this->assign('show',$show);
		$this->assign('product',$product);
		$this->assign('a',$page->firstRow+1);
		$this->display();
    }
    public function product_add(){
		$cat_pro=M('cat_pro')->select();
		$this->assign('cat_pro',$cat_pro);
		if(I('post.')){
			$data=array(
				'pro_name'=>I('post.pro_name'),
				'pro_introduction'=>I('post.pro_introduction'),
				'pro_details'=>I('post.pro_details'),
				'pro_parameter'=>I('post.pro_parameter'),
				'is_show'=>I('post.is_show'),
				'pro_addtime'=>strtotime(I('post.pro_addtime')),
				'pro_ord'=>I('post.pro_ord'),
				'cat_id'=>I('post.cat_id'),
			);
			$upload=new \Think\Upload();
			$upload->exts=array('jpg','png','gif');
			$upload->rootPath='./Upload/';
			$upload->savePath='product/';
			$info=$upload->upload();
			if($info){
				$data['pro_img1']=$info['pro_img1']['savepath'].$info['pro_img1']['savename'];
				$data['pro_img2']=$info['pro_img2']['savepath'].$info['pro_img2']['savename'];
			}
			$res=M('product')->add($data);
			if($res){
				$this->success('添加成功',U('Admin/Product/product_list'));
				exit();
			}else{
				$this->error('数据输入有误');
				exit();
			}
		}
		$this->display();
	}
	public function product_edit(){
		$cat_pro=M('cat_pro')->select();
		$this->assign('cat_pro',$cat_pro);
		$aid=I('get.aid');
		$product=M('product')->where("pro_id=$aid")->find();
		$this->assign('product',$product);
		if(I('post.')){
			$data=array(
				'pro_name'=>I('post.pro_name'),
				'pro_introduction'=>I('post.pro_introduction'),
				'pro_details'=>I('post.pro_details'),
				'pro_parameter'=>I('post.pro_parameter'),
				'is_show'=>I('post.is_show'),
				'pro_addtime'=>strtotime(I('post.pro_addtime')),
				'pro_ord'=>I('post.pro_ord'),
				'cat_id'=>I('post.cat_id'),
			);
			$pro_id=I('post.pro_id');
			if($_FILES['pro_img2']['size'] && $_FILES['pro_img1']['size']) {
				$old_arr=M('product')->field("pro_img1,pro_img2")->where("pro_id=$pro_id")->find();
				$path='./Upload/'.$old_arr['pro_img1'];
				if(!empty($old_arr) && file_exists($path)){
					unlink($path);
				}
				$path2='./Upload/'.$old_arr['pro_img2'];
				if(!empty($old_arr) && file_exists($path2)){
					unlink($path2);
				}
				$upload=new \Think\Upload();
				$upload->exts=array('jpg','png','gif');
				$upload->rootPath='./Upload/';
				$upload->savePath='product/';
				$info=$upload->upload();
				if($info){
					$data['pro_img1']=$info['pro_img1']['savepath'].$info['pro_img1']['savename'];
					$data['pro_img2']=$info['pro_img2']['savepath'].$info['pro_img2']['savename'];
				}
			}elseif($_FILES['pro_img1']['size'] ){
				$old_arr=M('product')->field("pro_img1")->where("pro_id=$pro_id")->find();
				$path='./Upload/'.$old_arr['pro_img1'];
				if(!empty($old_arr) && file_exists($path)){
					unlink($path);
				}
				$upload=new \Think\Upload();
				$upload->exts=array('jpg','png','gif');
				$upload->rootPath='./Upload/';
				$upload->savePath='product/';
				$info=$upload->upload();
				if($info['pro_img1']){
					$data['pro_img1']=$info['pro_img1']['savepath'].$info['pro_img1']['savename'];
				}
			}elseif($_FILES['pro_img2']['size']){
				$old_arr=M('product')->field("pro_img2")->where("pro_id=$pro_id")->find();
				$path='./Upload/'.$old_arr['pro_img2'];
				if(!empty($old_arr) && file_exists($path)){
					unlink($path);
				}
				$upload=new \Think\Upload();
				$upload->exts=array('jpg','png','gif');
				$upload->rootPath='./Upload/';
				$upload->savePath='product/';
				$info=$upload->upload();
				if($info['pro_img2']){
					$data['pro_img2']=$info['pro_img2']['savepath'].$info['pro_img2']['savename'];
				}
			}
			$res=M('product')->where("pro_id=$pro_id")->save($data);
			if($res){
				$this->success('编辑成功',U('Admin/Product/product_list'));
				exit();
			}else{
				$this->error('数据输入有误');
				exit();
			}
		}
		$this->display();
	}
	public function product_del(){
		$pro_id=I('get.a_id');
		$old_arr=M('product')->field("pro_img1")->where("pro_id=$pro_id")->find();
		$path='./Upload/'.$old_arr['pro_img1'];
		if(!empty($old_arr) && file_exists($path)){
			unlink($path);
		}
		$old_arr2=M('product')->field("pro_img2")->where("pro_id=$pro_id")->find();
		$path2='./Upload/'.$old_arr2['pro_img2'];
		if(!empty($old_arr2) && file_exists($path2)){
			unlink($path2);
		}
		$res=M('product')->where("pro_id=$pro_id")->delete();
		if($res){
			echo 1;exit();
		}else{
			echo M('product')->getLastSql();exit();
		}
	}
	public function name_ajax(){
		// 双击修改标题的ajax
		if(!empty(I('get.value'))){
		    $id=I('get.id');
		    $value=I('get.value');
		    $data=array(
		      'pro_name'=>$value,
		    );
		    $res=M('product')
		    		->where("pro_id=$id")
		    		->save($data);
		    if($res){
		      echo 1;die;
		    }else{
		      echo 2;die;
		    }
		  }
	}
	public function show_ajax(){
		// 是否上架的ajax
		if(!empty($_GET['show'])){
		    $id=$_GET['id'];
		    $show=$_GET['show'];
		    if($show==1){
		      $data=array(
		        'is_show'=>2,
		      );
		    }else{
		      $data=array(
		        'is_show'=>1,
		      );
		    }
		    $res=M('product')->where("pro_id=$id")->save($data);
		    if($res){
		      echo 1;die;
		    }else{
		      echo 2;die;
		    }
		  }
	}
	public function quandel(){
		
	}

}